import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;

public class num503 {
    public int[] nextGreaterElements(int[] nums) {
        //循环数组中的下一个更大元素
        int[] arr = new int[nums.length];
        Arrays.fill(arr,-1);
        Deque<Integer> stack = new LinkedList<>();
        for (int i = 0; i < (2 * nums.length); i++) {
            while (!stack.isEmpty() && nums[i % nums.length] > nums[stack.peek()]) {
                arr[stack.pop()] = nums[i % nums.length];
            }
            stack.push(i % nums.length);
        }
        return arr;
    }
}
